package com.company.example.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.company.example.core.util.StringUtil;
import com.company.example.model.pojo.Account;
import com.company.example.model.pojo.Student;
import com.company.example.service.MUserService;
import com.company.example.service.MongoDBService;
import com.mongodb.BasicDBObject;

/**
 * @package   com.company.example.controller
 * @file MongoDBController.java
 * @author   yanwl
 * @date 2016年9月30日
 * @version V 1.0
 */
//@Controller
@RequestMapping("/mongo")
public class MongoDBController {
	@Autowired
	private MongoDBService mongoDBService;
	@Autowired
	private MUserService mUserService;
	
	private int pageSize = 2;
	private int pageNo = 1;
	
	/**
	 * @return mongo查询测试
	 * @throws Exception
	 */
	@RequestMapping(value = "/select.htm", method = RequestMethod.GET)
	@ResponseBody
	public void mongoSelect(HttpServletRequest request) throws Exception {
		Query query = new Query();
		
		//分页查询
		if(StringUtil.isNotEmpty(request.getParameter("pageNo"))) {
			pageNo = Integer.valueOf(request.getParameter("pageNo"));
		}
		//分页、排序
		query.skip((pageNo - 1) * pageSize).limit(pageSize).with(new Sort(new Sort.Order(Direction.DESC, "createTime")));
		
		List<Account> accouts = mongoDBService.find(query, Account.class);
		for (Account account : accouts) {
			System.out.println(account.getId()+"--"+account.getName()+"--"+account.getCreateTime()+"--"+account.getAge());
		}
		
		/*List<Account> accouts = mUserService.selectAccount();
		for (Account account : accouts) {
			System.out.println(account.getId()+"--"+account.getName()+"--"+account.getCreateTime()+"--"+account.getAge());
		}*/
		
		/*Student s = mongoDBService.findById(Student.class, "2");
		System.out.println(s);*/

//		query.addCriteria(new Criteria("id").lt("3").gte("2"));
//		query.addCriteria(new Criteria("name").is("ls"));
//		System.out.println(mongoDBService.count(query, Account.class));
	}
	
	/**
	 * @return mongo新增测试
	 * @throws Exception
	 */
	@RequestMapping(value = "/insert.htm", method = RequestMethod.GET)
	@ResponseBody
	public void mongoInsert() throws Exception {
		Account acc = new Account();
//		acc.setId("5");
		acc.setAge(27);
		acc.setCreateTime(new Date());
		acc.setName("cc");
		mongoDBService.insert(acc);
		
		/*List<Object> list = new ArrayList<Object>();
		for (int i = 1; i < 1000001; i++) {
			Student s = new Student();
			s.setAge(i);
			s.setCreateTime(new Date());
			s.setId(i+"");
			s.setName("s"+i);
			list.add(s);
		}
		mongoDBService.batchSave(list);*/
	}
	
	/**
	 * @return mongo修改测试
	 * @throws Exception
	 */
	@RequestMapping(value = "/update.htm", method = RequestMethod.GET)
	@ResponseBody
	public void mongoUpdate() throws Exception {
		/*Account acc = new Account();
		acc.setId("5");
		acc.setAge(27);
		acc.setCreateTime(new Date());
		acc.setName("bb1");
		mongoDBService.saveOrUpdate(acc);*/
		
		Query query = new Query();
		query.with(new Sort(new Sort.Order(Direction.ASC, "createTime"))).addCriteria(new Criteria("age").is(27));
		Update update = new Update();
		update.set("name", "aa");
		
		mongoDBService.update(query, update, Account.class);
	}
	
	/**
	 * @return mongo删除测试
	 * @throws Exception
	 */
	@RequestMapping(value = "/delete.htm", method = RequestMethod.GET)
	@ResponseBody
	public void mongoDelete() throws Exception {
		Student s = new Student();
		s.setId("1");
		mongoDBService.delete(s);
	}
}
